

const webpack = require('webpack');
const request = require('request');


const config = require('./webpack.config.dev');
const compiler = webpack(config);
const devMiddleware = require('webpack-dev-middleware');
let hotMiddleware = require('webpack-hot-middleware');
const devFunc = devMiddleware(compiler);

const proxy = require('./proxy')

let hotFunc = hotMiddleware(compiler, {
    log: console.log,
    path: '/__webpack_hmr',
    heartbeat: 2000
});


const express = require('express');
const app = express();

for (let url of proxy.proxyUrls) {
    app.use(url, function (req, res) {
        console.log(req)
        req.pipe(request(proxy.host + req.originalUrl)).pipe(res)
    })

}

app.use(devFunc);
app.use(hotFunc);
app.use(express.static('static'));

/*
compiler.plugin('compilation',function(compilation){
    console.log(compilation);
})*/


compiler.hooks.compilation.tap("html-webpack-plugin", function (HtmlwebpackPlugin) {
    console.log('模块构建')
    HtmlwebpackPlugin.hooks.htmlWebpackPluginAfterEmit.tapAsync('hotFunc', function (data, cb) {
        console.log('页面构建');
        console.log(hotFunc.publish)
        hotFunc.publish({ action: 'reload' });
        cb();
    });
});

app.listen(3000);